package edu.itesm.rm.scene {
	import com.greensock.TweenMax;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.display.BitmapData;
	import away3d.materials.BitmapMaterial;
	import flash.events.Event;
	import edu.itesm.rm.utils.MaterialUtils;
	import away3d.materials.AnimatedBitmapMaterial;
	import away3d.primitives.Plane;
	import away3d.materials.MaterialBase;

	/**
	 * @author aldoreyes
	 */
	public class WallTarget extends Plane {
		
		[Embed(source="../../../../../assets/target.png")]		
		private static var simple_target_material:Class;
		
		[Embed(source="../../../../../assets/target_destroyed.png")]		
		private static var target_destroyed_material:Class;
		
		public static const SIMPLE:int = 0; 
		public static const SHIELDED:int = 1;
		public static const TURRET:int = 2;
		
		public static const SIMPLE_LIFE:int = 5;
		public static const SHIELDED_LIFE:int = 12;
		public static const TURRET_LIFE:int = 8;
		
		private static const MAT_WIDTH:int = 16;
		
		public static const TARGET_WIDTH:int =50;
		public static const TARGET_HALF_WIDTH:int =25;
		public static const DESTROYED:String = "destroyed";
		
		private static var simple_mat:AnimatedBitmapMaterial;
		private static var destroyed_mat:BitmapMaterial;
		
		private static var shielded:AnimatedBitmapMaterial;
		private static var shielded1:AnimatedBitmapMaterial;
		private static var shielded2:AnimatedBitmapMaterial;
		
		private static var turret_mat:BitmapMaterial;
		
		private var _destroyed:Boolean;
		
		private var _life:int;
		private var _type:int;
		private var _ShootDelay:Number;
		private var _ShootFrequency:Number;
		private var _bullet:Bullet;
		
		public function get destroyed() : Boolean {
			return _destroyed;
		}
		
		public function WallTarget(type:int = SIMPLE, shootDelay:Number = 0, shootFrequency:Number = 0) {
			super(type==SIMPLE?getSimpleMaterial():type==SHIELDED?getShieldedMaterial():getTurretMaterial(), TARGET_WIDTH, TARGET_WIDTH, 1, 1, true);
			_life = type==SIMPLE?SIMPLE_LIFE:SHIELDED_LIFE;
			_type = type;
			_destroyed = false;
			_ShootDelay = shootDelay;
			_ShootFrequency = shootFrequency;
			if(_type == TURRET){
				_bullet = new Bullet(0, 0, Bullet.ENEMY);
				_bullet.addEventListener(Bullet.HIT, handleBulletHit, false, 0, true);
				TweenMax.delayedCall(_ShootDelay, shoot);
			}
			
			
		}
		
		public function init(cameraZ:Number):void{
			if(_bullet){
				_bullet.endZ = cameraZ;
				_bullet.startZ = z+1;
				_bullet.x = x;
				_bullet.y = y;
				_bullet.z = z + 1;
				this.parent.addChild(_bullet);
			}
		}
		
		private function shoot():void {
			if(!destroyed){
				_bullet.shoot(this.x, this.y);
				TweenMax.delayedCall(_ShootFrequency, shoot);
			}else {
				if (_bullet.parent) {
					_bullet.parent.removeChild(_bullet);
				}	
			}
			
		}
		
		public function destroy():void 
		{
			if (_bullet) {
				TweenMax.killTweensOf(_bullet);
				if (_bullet.parent) {
					_bullet.parent.removeChild(_bullet);
				}
			}
			this.dispose(false);
		}
		
		private static function getSimpleMaterial():AnimatedBitmapMaterial{
			if(simple_mat == null){
				simple_mat = new AnimatedBitmapMaterial();
				simple_mat.setMaps(MaterialUtils.generateSequence(new simple_target_material().bitmapData, MAT_WIDTH, MAT_WIDTH, 3, 5));
				simple_mat.transparent = true;
				simple_mat.play();
				
				
			}
			
			if(!destroyed_mat){
				destroyed_mat = new BitmapMaterial(new target_destroyed_material().bitmapData);
				destroyed_mat.transparent = true;
			}
			
			
			return simple_mat;
		}
		
		private static function getShieldedMaterial():AnimatedBitmapMaterial{
			if(!shielded){
				var bmd:BitmapData = new simple_target_material().bitmapData;
				shielded = new AnimatedBitmapMaterial();
				shielded.setMaps(MaterialUtils.generateSequence(bmd, MAT_WIDTH, MAT_WIDTH, 3, 5, 16));
				shielded.transparent = true;
				shielded.play();
				
				shielded1 = new AnimatedBitmapMaterial();
				shielded1.setMaps(MaterialUtils.generateSequence(bmd, MAT_WIDTH, MAT_WIDTH, 3, 5, 32));
				shielded1.transparent = true;
				shielded1.play();
				
				shielded2 = new AnimatedBitmapMaterial();
				shielded2.setMaps(MaterialUtils.generateSequence(bmd, MAT_WIDTH, MAT_WIDTH, 3, 5, 48));
				shielded2.transparent = true;
				shielded2.play();
			}
			
			if(!destroyed_mat){
				destroyed_mat = new BitmapMaterial(new target_destroyed_material().bitmapData);
				destroyed_mat.transparent = true;
			}
			
			
			return shielded;
		}
		
		private static function getTurretMaterial():BitmapMaterial{
			if(!turret_mat){
				var bmd:BitmapData = new simple_target_material().bitmapData;
				var target:BitmapData = new BitmapData(16, 16);
				target.copyPixels(bmd, new Rectangle(0,64,16,16), new Point(0,0));
				turret_mat = new BitmapMaterial(target);
				turret_mat.transparent = true;
			}
			
			if(!destroyed_mat){
				destroyed_mat = new BitmapMaterial(new target_destroyed_material().bitmapData);
				destroyed_mat.transparent = true;
			}
			
			return turret_mat;
		}
		
		public function hit():void{
			_life--;
			if(_type == SHIELDED){
				if(_life == 9){
					material = shielded1;
				}else if(_life == 6){
					material = shielded2;
				}else if(_life == 3){
					material = simple_mat;
				}
			}
			if(_life==0){
				_destroyed = true;
				material = destroyed_mat;
				if(parent){
					this.parent.dispatchEvent(new Event(DESTROYED, true, true));
				}
			}
		}
		
		private function handleBulletHit(event:Event):void 
		{
			if(parent){
				parent.dispatchEvent(event.clone());
			}
		}
	}
}
